home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / RotateString / RotateString.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.7 KB  |  202 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        RotateString.c
  3.  
  4.     Contains:    This function will return a BitMap that contains the
  5.                 text passed rotated 90°
  6.     
  7.                 The font characteristics are copied from
  8.                 the current port before creating the BitMap.
  9.     
  10.  
  11.     Written by: RT and BS
  12.  
  13.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  14.  
  15.                 You may incorporate this Apple sample source code into your program(s) without
  16.                 restriction. This Apple sample source code has been provided "AS IS" and the
  17.                 responsibility for its operation is yours. You are not permitted to redistribute
  18.                 this Apple sample source code as "Apple sample source code" after having made
  19.                 changes. If you're going to re-distribute the source, we require that you make
  20.                 it clear in the source that the code was descended from Apple sample source
  21.                 code, but that you've made changes.
  22.  
  23.     Change History (most recent first):
  24.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  25.                                             for demonstration purposes.
  26.                 7/14/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  27.                 
  28.                 8/27/92        BS                added a direction parameter to the end. It will 
  29.                                             rotate in the direction you specify. ALSO fixed 
  30.                                             some LAME bugs, such as the rotate routine did not 
  31.                                             preserve the current port,and it also did not dispose 
  32.                                             of a temporary bitmap it used.  Should be in good
  33.                                             shape now.
  34.  
  35. */
  36. #include "CarbonPrefix.h"
  37. #include <Memory.h>
  38. #include <QDOffscreen.h>
  39. #include "RotateString.h"
  40.  
  41. /*
  42.     In pascal this is:
  43.  
  44.  function RotateString (str: Str255; var destMap: BitMap, direction : integer): OSErr;
  45.  
  46. */
  47.  
  48.  
  49. pascal OSErr RotateString( Str255 str, BitMap *destMap, short direction)
  50. {
  51.     //GrafPort    srcPort;
  52.     //GrafPtr        srcPortPtr;
  53.     char        *srcPtr, *destPtr;
  54.     FontInfo    f;
  55.     short        heightBits, heightBytes;
  56.     short        width, widthBytes;
  57.     short        rotateWidth;
  58.     short        rotateHeight;
  59.     short        row, column;
  60.     short        texFont,texFace,texMode,texSize;    
  61.     short        err;
  62.     //Rect        tempRect1;
  63.     RgnHandle    rgnHandle = NewRgn();
  64.     CGrafPtr    currentPort;        /* Saved CGrafPtr for later restore. */
  65.     GDHandle    currentDevice;        /* Saved device for later restore. */
  66.     GWorldPtr    sourceGWorld;
  67.     Rect        rect;
  68.     int            sourceRowBytes;
  69.     
  70.     
  71.     GetGWorld( ¤tPort, ¤tDevice );
  72.     
  73.     /*texFont = qd.thePort->txFont;
  74.     texFace = qd.thePort->txFace;
  75.     texMode = qd.thePort->txMode;
  76.     texSize = qd.thePort->txSize;*/
  77.     
  78.     texFont = GetPortTextFont(GetQDGlobalsThePort());
  79.     texFace = GetPortTextFace(GetQDGlobalsThePort());
  80.     texMode = GetPortTextMode(GetQDGlobalsThePort());
  81.     texSize = GetPortTextSize(GetQDGlobalsThePort());
  82.     
  83.     //OpenPort( &srcPort);
  84.     //srcPortPtr = CreateNewPort();
  85.     //SetPort(&srcPort);
  86.     //SetPort(srcPortPtr);
  87.     
  88.     TextFont(texFont);    
  89.     TextFace(texFace);    
  90.     TextMode(texMode);    
  91.     TextSize(texSize);
  92.     GetFontInfo( &f);
  93.     
  94.     heightBits = f.ascent + f.descent + f.leading;
  95.     heightBytes = ((heightBits + 7) & 0x7FF8) >> 3;
  96.     width = StringWidth( str );
  97.     widthBytes = ((width + 7) & 0x7FF8) >> 3;
  98.  
  99.     rotateWidth = heightBytes;
  100.     rotateHeight = widthBytes << 3;
  101.     
  102.     //srcPort.portBits.baseAddr = NewPtrClear( widthBytes * heightBits);
  103.     /*(*(GetPortPixMap(srcPortPtr)))->baseAddr = NewPtrClear( widthBytes * heightBits );
  104.     if(err = MemError()) return err;
  105.  
  106.     //srcPort.portBits.rowBytes = widthBytes;
  107.     (*(GetPortPixMap(srcPortPtr)))->rowBytes = widthBytes;
  108.     //SetRect( &srcPort.portBits.bounds, 0, 0, width, heightBits);
  109.     SetRect( GetPortBounds(srcPortPtr, &tempRect1), 0, 0, width, heightBits );*/
  110.     SetRect( &rect, 0, 0, width, heightBits );
  111.     NewGWorld( &sourceGWorld, 1, &rect, nil, nil, 0 );
  112.     SetGWorld( sourceGWorld, nil );
  113.     EraseRect(&rect);
  114.     
  115.     TextFont(texFont);    
  116.     TextFace(texFace);    
  117.     TextMode(texMode);    
  118.     TextSize(texSize);
  119.     
  120.     sourceRowBytes = GetPixRowBytes(GetGWorldPixMap(sourceGWorld));
  121.     
  122.     destMap->baseAddr = NewPtrClear( rotateWidth * rotateHeight);
  123.     if(err = MemError()) {
  124.         //DisposePtr(srcPort.portBits.baseAddr);
  125.         DisposeGWorld(sourceGWorld);
  126.         return err;
  127.     }
  128.     
  129.     destMap->rowBytes = rotateWidth;
  130.     SetRect( &destMap->bounds, 0, 0, rotateWidth * 8, rotateHeight);
  131.     
  132.     //RectRgn(srcPort.visRgn,&srcPort.portBits.bounds);
  133.     RectRgn(GetPortVisibleRegion(sourceGWorld, rgnHandle), &(*(GetGWorldPixMap(sourceGWorld)))->bounds);
  134.     MoveTo( 0, f.ascent);  // change 2.0.1 <CKH> was heightBits
  135.     DrawString( str );
  136.  
  137.     
  138.     /*****************************************/
  139.     /*  Rotate Algorithm                     */
  140.     /*****************************************/
  141.  
  142.     LockPixels(GetGWorldPixMap(sourceGWorld));
  143.     //srcPtr = srcPort.portBits.baseAddr;        // Start at the Top,Left
  144.     srcPtr = (*(GetGWorldPixMap(sourceGWorld)))->baseAddr;
  145.     
  146.     if(direction == counterClockWise)        // Start at the Bottom,Left
  147.         destPtr = destMap->baseAddr + ((rotateHeight-1) * rotateWidth);
  148.     else                                    // Start at the Top,Left
  149.         destPtr = destMap->baseAddr;
  150.                                             
  151.     for( column = 0; column < width; column++)        /* width is in pixels */
  152.     {
  153.         for( row = 0; row < heightBits; row++)        /* height in pixels, too */
  154.         {
  155.         
  156.             if(direction == counterClockWise) {
  157.                 /* We are rotating left ( counter clock-wise) thus */
  158.                 /*  as we search across the top of the horizontal pixel bit map,    */
  159.                 /*    we write down the right edge of the vertical pixel bit map */
  160.  
  161.                 if( *(srcPtr                        /* Begin with the upper left corner */ 
  162.                         //+ (row*widthBytes)             /* Move down a row */
  163.                         + (row * sourceRowBytes)
  164.                         + (column >> 3))            /* Go to the first even byte */
  165.                         & (0x80>>(column & 7)) )    /* calculate a shifted bit position based on the column */
  166.                     {
  167.                         *(destPtr                     /* Begin in the lower left corner */
  168.                             - (column*rotateWidth)     /* Subtract, move up for each row */
  169.                             + (row >> 3))             /* Calculate a bit position for the column */
  170.                             |= (0x80>>(row & 7));    /* Set a bit based on the column */
  171.                     }
  172.             } else {    /* clockWise */
  173.                 /*  We are rotating right (clock-wise) thus */
  174.                 /*  as we search across the top of the horizontal pixel bit map,    */
  175.                 /*    we write down the left edge of the vertical pixel bit map */
  176.                 
  177.                 if( *(srcPtr                        /* Begin with the upper left corner */ 
  178.                         //+ (row*widthBytes)             /* Move down a row */
  179.                         + (row * sourceRowBytes)
  180.                         + (column >> 3))            /* Go to the first even byte */
  181.                         & (0x80>>(column & 7)) )    /* calculate a shifted bit position based on the column */
  182.                     {
  183.                         *(destPtr                     /* Begin in the lower left corner */
  184.                         + (column*rotateWidth) /* Add, move down for each row */
  185.                         - (row >> 3))             /* Calculate a bit position for the column */
  186.                         |= (0x01<<(row & 7));    /* Set a bit based on the column */
  187.                     }
  188.             }
  189.         }
  190.     }
  191.     
  192.     /* clean up the used source BitMap, and restore the ports properly */    
  193.     //DisposePtr(srcPort.portBits.baseAddr);
  194.     UnlockPixels(GetGWorldPixMap(sourceGWorld));
  195.     DisposeGWorld(sourceGWorld);
  196.     //ClosePort(&srcPort);
  197.     SetGWorld( currentPort, currentDevice );
  198.     DisposeRgn(rgnHandle);
  199.     
  200.     return noErr;
  201. }
  202.